import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rjpb/page/home/inspection/inspection_page.dart';
import 'package:rjpb/page/home/fireAlarm/fire_alarm_page.dart';
import 'package:rjpb/page/home/profile/profile_page.dart';
import 'package:rjpb/page/home/controllers/home_controller.dart'; // 导入 HomeController
import 'package:rjpb/components/fire_alarm_alert.dart';

class HomePage extends StatelessWidget {
  final HomeController controller = Get.put(HomeController()); // 初始化控制器

  final List<Widget> _pages = [
    InspectionPage(),
    FireAlarmPage(),
    ProfilePage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: null,
      body: Stack(
        children: [
          SafeArea(
            child: PageView(
              controller: controller.pageController,
              physics: NeverScrollableScrollPhysics(), // 禁用手动滑动
              onPageChanged: (index) {
                controller.changePage(index); // 更新当前页面索引并触发加载逻辑
              },
              children: _pages,
            ),
          ),
          const FireAlarmAlert(), // 添加全局火警弹窗
        ],
      ),
      bottomNavigationBar: Obx(() => Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
              boxShadow: [
                BoxShadow(
                  color: Colors.black12,
                  blurRadius: 10,
                  spreadRadius: 2,
                )
              ],
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
              child: BottomNavigationBar(
                currentIndex: controller.currentIndex.value,
                onTap: (index) {
                  controller.changePage(index);
                  controller.pageController.animateToPage(
                    index,
                    duration: Duration(milliseconds: 300),
                    curve: Curves.easeInOut,
                  );
                },
                items: [
                  BottomNavigationBarItem(
                    icon: Icon(Icons.search),
                    activeIcon: Container(
                      padding: EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Theme.of(context).primaryColor,
                        shape: BoxShape.circle,
                      ),
                      child: Icon(Icons.search, color: Colors.white),
                    ),
                    label: '巡检',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.warning),
                    activeIcon: Container(
                      padding: EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Theme.of(context).primaryColor,
                        shape: BoxShape.circle,
                      ),
                      child: Icon(Icons.warning, color: Colors.white),
                    ),
                    label: '火警',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.person),
                    activeIcon: Container(
                      padding: EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Theme.of(context).primaryColor,
                        shape: BoxShape.circle,
                      ),
                      child: Icon(Icons.person, color: Colors.white),
                    ),
                    label: '个人中心',
                  ),
                ],
                type: BottomNavigationBarType.fixed,
                selectedItemColor: Theme.of(context).primaryColor,
                unselectedItemColor: Colors.grey,
                showSelectedLabels: true,
                showUnselectedLabels: true,
                elevation: 10,
              ),
            ),
          )),
    );
  }
}
